/************************************************************************
Purpose: 	Create Plant-Period-Date-Bid Panel
*************************************************************************/

set more off
clear all
pause on

use "$TRADING_DATA_OUT/record_plant-commodity-date-bid.dta", clear

rename commodity_period period
merge m:1 period using "$TRADING_DATA_OUT/index_period.dta"
drop _merge
drop if missing(bid_id)
sort gpcb_id period date bid_id

** generate plant x period index for plant x period FEs
egen plant_period_id = group(gpcb_id period)

********************************************************************************
*** Delete rejected bids, and create relevant variables for analysis
********************************************************************************

drop if bid_status == "REJ"
drop if missing(bid_qty)

** Generate auction indicator
gen week_days = dow(date)
bysort commodity date: egen min_bid_price = min(bid_price)
bysort commodity date: egen max_bid_price = max(bid_price)
gen is_auction = 0
replace is_auction = 1 if min_bid_price != max_bid_price
replace is_auction = 0 if date == td(08jan2020) & commodity == "PSUM161119"
replace is_auction = 0 if date == td(07feb2020) & commodity == "PSUM010120"
replace is_auction = 0 if date == td(07mar2020) & commodity == "PSUM010220"
drop min_bid_price max_bid_price
label var is_auction "=1 if placed on an auction day" 

// tab is_auction
//   Auction=1 |      Freq.     Percent        Cum.
// ------------+-----------------------------------
//           0 |      5,637       66.84       66.84
//           1 |      2,796       33.16      100.00
// ------------+-----------------------------------
//       Total |      8,433      100.00


** Generate week variables
gen bid_day_temp = mod(bid_day, 7) - 1
gen first_day_week = mod(week_days - bid_day_temp, 7)
gen week_num = floor((bid_day + first_day_week - 1) / 7) + 1
gen week_day_num =  week_num + (week_days - 1) / 7 
bysort period: egen max_week_num = max(week_num)
gen week_left = max_week_num - week_num
drop bid_day_temp first_day_week week_days
label var week_num "Number of weeks the bid is placed in the period"
label var week_day_num "=week_num + (date - 1) / 7 (Monday is the first day of week)"
label var max_week_num "Number of weeks in the period"
label var week_left "Number of weeks left until the period ends."

** Generate permit holdings if a bid is executed
gen permit_holding_hyp = permit_holding
replace permit_holding_hyp = permit_holding_hyp + bid_qty if missing(trade_qty)
replace permit_holding_hyp = permit_holding_hyp + bid_qty - trade_qty if !missing(trade_qty) & !missing(bid_qty) 
label var permit_holding_hyp "Permit holdings if a bid is executed (kg)"

** Generate log variables
gen ln_permit_holding_hyp = ln(permit_holding_hyp)
gen ln_bid_price = ln(bid_price)
label var ln_permit_holding_hyp "log[permit_holding_hyp (kg)]"
label var ln_bid_price "log[bid_price (Rs)]"

sort gpcb_id period date bid_id

********************************************************************************
*** Merge baseline covariates
********************************************************************************
merge m:1 gpcb_id using "$TRADING_DATA_OUT/covariates_plant.dta"
drop _merge
sort gpcb_id period date bid_id


********************************************************************************
*** Merge potential max emissions for abatement costs panel
********************************************************************************
merge m:1 gpcb_id using "$EMISSIONS_DATA_OUT/potential_max_emissions.dta", ///
	keepusing(AverageUncontrolledMass12 AverageUncontrolledMass16) assert(2 3) keep(3) nogen
rename AverageUncontrolledMass12 pm_mass_potential_max_12
rename AverageUncontrolledMass16 pm_mass_potential_max_16

save "$TRADING_DATA_CLEAN/panel_plant-period-date-bid.dta", replace
